package com.hdc.hdc.warehouseManage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hdc.hdc.warehouseManage.entity.Inorder;
import com.hdc.hdc.warehouseManage.entity.InorderAndGoods;
import com.hdc.hdc.warehouseManage.entity.vo.OrderVo;
import com.hdc.hdc.warehouseManage.repository.InorderAndGoodsRepository;
import com.hdc.hdc.warehouseManage.repository.InorderRepository;
import com.hdc.hdc.warehouseManage.service.InorderServiceI;
import com.hdc.hdc.warehouseManage.utill.IsNotNullValit;

/**
 * 
 * @author gxy
 *	2018年4月25日
 *	hdc.warehouseManage
 *
 */
@Service
public class InorderServiceImpl implements InorderServiceI {
	
	@Autowired
	private InorderRepository inorderRepository;
	
	@Resource
    private EntityManagerFactory sessionFactory;
	
	@Autowired
	private InorderAndGoodsRepository inorderAndGoodsRepository;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Inorder> findInorderDetailByQuery(OrderVo vo) {
		
		List<Inorder> results = new ArrayList<Inorder>(10);
		
		EntityManager em = this.sessionFactory.createEntityManager();
		
		Map<String, String> map = new HashMap<String, String>(10);
		
		StringBuffer hql = new StringBuffer("select t from Inorder t left join InorderAndGoods i on t.inorderNum = i.inordernum ");
		
		hql.append(" where 1=1");
		
		if(IsNotNullValit.StringIsNotNull(vo.getToWhereHouse())) {
			
			hql.append(" and t.toWhereHouse = :toWhereHouse");
			map.put("toWhereHouse", vo.getToWhereHouse());
			
		}
		
		if(IsNotNullValit.StringIsNotNull(vo.getToWherePosition())) {
			
			hql.append(" and t.toWherePosition = :toWherePosition");
			map.put("toWherePosition", vo.getToWherePosition());
			
		}
		
		if(IsNotNullValit.StringIsNotNull(vo.getToWhereLocation())) {
			
			hql.append(" and t.toWhereLocation = :toWhereLocation");
			map.put("toWhereLocation", vo.getToWhereLocation());
			
		}
		
		if(IsNotNullValit.StringIsNotNull(vo.getInorderNum())) {
			
			hql.append(" and t.inorderNum = :inorderNum");
			map.put("inorderNum", vo.getInorderNum());
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getGoodsName())) {
			
			hql.append(" and i.goodsName like  :goodsName");
			map.put("goodsName", "%"+vo.getGoodsName()+"%");
		}
		
		if (IsNotNullValit.StringIsNotNull(vo.getSku())) {
			
			hql.append(" and i.goodsSku like :goodsSku");
			map.put("goodsSku", "%"+vo.getSku()+"%");
		}
		
		Query query = em.createQuery(hql.toString());
		
		Iterable<String> keys = map.keySet();
		for (String key : keys) {
			if(IsNotNullValit.StringIsNotNull(key)) {
				query.setParameter(key, map.get(key));
			}
		}
		
		query.setFirstResult((vo.getPageNum()-1)*vo.getPageSize());
		query.setMaxResults(vo.getPageSize());
		
		results = query.getResultList();
		
		return results;
	}

	@Override
	public void saveInorderAndGoods(OrderVo vo) {

		Inorder inorder = new Inorder();
		
		BeanUtils.copyProperties(vo, inorder);
		
		InorderAndGoods iag = new InorderAndGoods();
		
		BeanUtils.copyProperties(vo, iag);
		inorder.setStatus(0);
		inorder.setUpdator(inorder.getCreator());
		inorder.setUpdatorName(inorder.getUpdatorName());
		inorder.setUpdateDate(inorder.getUpdateDate());
		iag.setInordernum(inorder.getInorderNum());
		this.inorderRepository.save(inorder);
		this.inorderAndGoodsRepository.save(iag);
	}
	
	@Override
	public List<InorderAndGoods> findInorderGoodsDetailByInorderNum(String inorderNum) {
		return this.inorderAndGoodsRepository.findByInordernum(inorderNum);
	}

}
